﻿using System;
using System.Collections.Generic;
using System.Text;

using System.Net.Sockets; //使用到TcpListen类 
using System.Threading; //使用到线程 
using System.IO; //使用到StreamWriter类 
using System.Net; //使用IPAddress类、IPHostEntry类等 
using System.Configuration;

namespace LinkUp
{
    /// <summary>
    /// 发送排行榜
    /// </summary>
    public class SendRank
    {
        /// <summary>
        /// 连接默认主机
        /// </summary>
        public SendRank()
        {
            try
            {
                XmlRW xml = new XmlRW(AppDomain.CurrentDomain.BaseDirectory + "\\app.config");
                ipRemote = IPAddress.Parse(xml.GetValue("IP"));
                port = Convert.ToInt32(xml.GetValue("Port"));
            }
            catch
            {
                throw new Exception("无法读取配置文件！");
            }
        }

        /// <summary>
        /// 连接
        /// </summary>
        /// <returns></returns>
        public bool Connect()
        {
            try
            {
                tcpClient = new TcpClient();
                tcpClient.Connect(ipRemote, port);//对远程主机的端口提出TCP连接申请 

                nsStream = tcpClient.GetStream();//通过申请，并获取传送数据的网络基础数据流　　 
                tcpConnect = true;
                return true;
            }
            catch
            {
                throw new Exception("与服务器连接失败！");
            }
        }

        /// <summary>
        /// 发送数据 
        /// </summary>
        /// <param name="obj"></param>
        public bool Send(IList<User> list)
        {
            string userInfo = string.Empty;
            byte[] data = null;
            byte[] status = new byte[1];
            foreach (User u in list)
            {
                if (!u.IsUpload)
                {
                    userInfo = userInfo + u.ID + "\t" + u.Score.ToString() + "\n";
                }
            }
            if (userInfo != string.Empty)
            {
                userInfo = userInfo.TrimEnd('\n');
                data = ASCIIEncoding.Default.GetBytes(userInfo);
                nsStream.Write(data, 0, data.Length);
                nsStream.Read(status, 0, status.Length);
                string stat = ASCIIEncoding.Default.GetString(status);
                if (stat == "1")
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return true;
            }
        }

        /// <summary>
        /// 清理所有正在使用的资源。
        /// </summary> 
        public void Close()
        {
            if (tcpConnect)
            {
                nsStream.Close(); //清除资源 
                tcpClient.Close();
            }
        }

        #region field and property
        IPAddress ipRemote; //  IP协议地址
        int port; //端口
        private NetworkStream nsStream; //创建发送数据的网络基础数据流　 
        private TcpClient tcpClient;
        private bool tcpConnect = false; //定义标识符，用以表示TCP连接是否建立 
        #endregion
    }
}
